(function ($, win) {

    function Stir(element, options) {
        var _this = this;
        this.el = $(element);
        this.$body=$("html ,body")
        this.options = $.extend({},Stir.DEFAULTS, options || {});
        this.num=0
        this.sign=true
        this.init()
        _this.el.find(_this.options.upEvent).on(_this.options.event,function(){
            _this.upEventfn()
        });
        _this.el.find(_this.options.downEvent).on(_this.options.event,function () {
            _this.downEventfn()
        })
    }
    Stir.DEFAULTS={
        upEvent:".upEvent",
        downEvent:".downEvent",
        page:".page",
        event:"click",
        delay:1000
    };

    Stir.prototype.init=function () {
        var _op = this.options;
        this.el.find(_op.page)
            .css({"min-height":$(win).height()})
            .eq(this.num).show()
        //初始化
        this.animatefn(this.num+1,this.num)

    };
    Stir.prototype.downEventfn=function () {
        var _this = this;
        var _op = this.options;
        var len = this.el.find(_op.page).length-1
        var lnum = 0;
        if(!(this.num < len && this.sign)){
            return
        }
            _this.sign = false;
            lnum = _this.num;
                _this.num++;
            _this.el.find(_op.page).eq(_this.num).show()
            this.animatefn(lnum ,_this.num)
    }
    Stir.prototype.upEventfn=function () {
        var _this = this;
        var _op = this.options;
        var lnum = 0;
        if(!(this.num > 0 && this.sign)){
            return
        }
            _this.sign = false
            lnum = _this.num;
            _this.num--


            _this.el.find(_op.page).eq( _this.num).show()
            this.$body.scrollTop(_this.el.find(_op.page).eq(lnum).offset().top)

            this.animatefn(lnum ,_this.num)


    }

    Stir.prototype.animatefn=function (lpage ,cpage) {
        var _this = this;
        var _op = this.options;

        this.$body.stop().animate({
            scrollTop: _this.el.find(_op.page).eq( cpage).offset().top
        },_op.delay,function () {
            _this.watchEvent(cpage)
            _this.$body.scrollTop(_this.el.find(_op.page).eq(cpage).offset().top)
            _this.el.find(_op.page).eq( lpage).hide()
            _this.sign = true
        })
    }

    Stir.prototype.watchEvent=function (i) {
        var _op = this.options;
        var len = this.el.find(_op.page).length-1
        if(i==0){
            this.el.find(_op.upEvent).hide();
            this.el.find(_op.downEvent).show();

        }else if(i == len){
            this.el.find(_op.upEvent).show();
            this.el.find(_op.downEvent).hide();
        }else{
            this.el.find(_op.upEvent).show();
            this.el.find(_op.downEvent).show();
        }
    };

    // Stir plugin definition
    // =====================
    function Plugin(option) {
        return this.each(function(){
            //console.log($(this))
            new Stir($(this), option)
        })
    }
    $.fn.stir = Plugin;
    $.fn.stir.Constructor = Stir;





})(jQuery , window);


$(function () {
    $(".content").stir({
        upEvent:".btn_top",
        downEvent:".btn_next",
        delay:100
    })

})

